package com.woniuxy.main.handler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.session.Session;
import org.apache.shiro.web.session.HttpServletSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.main.pojo.Vi_order_coach;
import com.woniuxy.main.pojo.Vi_user_student;
import com.woniuxy.main.service.Vi_order_coachService;
import com.woniuxy.main.service.Vi_user_studentService;
import com.woniuxy.main.util.SessionUtil;

import lombok.Data;

@Controller
@RequestMapping("/myCoach")
@Data
public class Vi_order_coachHandler {
	@Resource
	private Vi_order_coachService vi_order_coachService;
	@Resource
	private Vi_user_studentService vi_user_studentService;
	
	// 查询我签约的教练
	@RequestMapping("/searchMyCoach")
	@ResponseBody
	public Map<String,Object> searchMyCoachBySid(int uid,Integer page) {
		// 根据uid查询sid或者gid
		Vi_user_student student = null;
		Session session = SessionUtil.getSession();
		if(session.getAttribute("userRole").equals("student")) {
			System.out.println(session.getAttribute("userRole"));
			// 当前登录账号为学生时
			student = vi_user_studentService.selectByUid(uid);
		} else {
			// 当前登录账号为场馆时
		}
		// 起始位置
		int index = (page - 1) * 8;
		// 查出的信息
		Map<String,Object> result = new HashMap<String,Object>();
		// 查询参数
		Map<String,Object> map = new HashMap<String,Object>();
		// 存入参数
		map.put("sid", student.getSid());
		map.put("index", index);
		// 查询总条数
		long totalCount = vi_order_coachService.countMyCoach(student.getSid());
		result.put("totalCount", totalCount);
		// 计算总页数
		long totalPage = totalCount % 8 == 0 ? totalCount / 8 : totalCount / 8 + 1;
		result.put("totalPage", totalPage);
		// 查询当前页的教练信息
		List<Vi_order_coach> myCoach = vi_order_coachService.searchMyCoachBySid(map);
		result.put("myCoach", myCoach);
		return result;
	}
}
